package cn.kgc.service.impl;

import cn.kgc.base.vo.SecKillSessionAndSecKillSkuVO;

import cn.kgc.base.vo.SecKillSkuInfoVo;
import cn.kgc.base.vo.SecKillSkuVo;
import cn.kgc.client.SeckillSkuClient;
import cn.kgc.commons.vo.Result;
import cn.kgc.dto.SmsSeckillSessionAddDTO;
import cn.kgc.dto.SmsSeckillSkuRelationAddDTO;
import cn.kgc.entity.SmsSeckillSkuRelation;
import cn.kgc.entity.SmsSkuFullReduction;
import cn.kgc.mapper.SmsSeckillSkuRelationMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.mapper.SmsSeckillSessionMapper;
import cn.kgc.entity.SmsSeckillSession;
import cn.kgc.service.SmsSeckillSessionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * (SmsSeckillSession)表服务实现类
 *
 * @author 石浩杰
 * @since 2023-01-07 09:36:59
 */
@Service("smsSeckillSessionService")
public class SmsSeckillSessionServiceImpl extends ServiceImpl<SmsSeckillSessionMapper, SmsSeckillSession> implements SmsSeckillSessionService {

    @Resource
    private SmsSeckillSessionMapper smsSeckillSessionMapper;

    @Resource
    private SmsSeckillSkuRelationMapper smsSeckillSkuRelationMapper;

    @Resource
    private SeckillSkuClient seckillSkuClient;

    @Transactional
    public boolean saveSessionAndSeckillSku(SmsSeckillSessionAddDTO smsSeckillSessionAddDTO) {
        SmsSeckillSession smsSeckillSession=new SmsSeckillSession();
        BeanUtils.copyProperties(smsSeckillSessionAddDTO,smsSeckillSession);
        smsSeckillSession.setStatus((short)1);
        smsSeckillSessionMapper.insert(smsSeckillSession);
        //场次对应的商品信息

        List<SmsSeckillSkuRelationAddDTO> list=smsSeckillSessionAddDTO.getSmsSeckillSkuRelationList();
        for (SmsSeckillSkuRelationAddDTO smsSeckillSkuRelationAddDTO : list) {
            SmsSeckillSkuRelation smsSeckillSkuRelation=new SmsSeckillSkuRelation();
            BeanUtils.copyProperties(smsSeckillSkuRelationAddDTO,smsSeckillSkuRelation);
            smsSeckillSkuRelation.setSessionId(smsSeckillSession.getId());
            smsSeckillSkuRelationMapper.insert(smsSeckillSkuRelation);

        }
        return true;
    }

    @Override
    public List<SecKillSessionAndSecKillSkuVO> getSecSessionAndSecSku() {
        List<SmsSeckillSession> smsSeckillSessionList =  getSmsSeckillSessions();
        List<SecKillSessionAndSecKillSkuVO> list=null;
        //获得今天的场次对应的商品信息
        list=smsSeckillSessionList.stream().map(smsSeckillSession -> {
            SecKillSessionAndSecKillSkuVO secKillSessionAndSecKillSkuVo=new SecKillSessionAndSecKillSkuVO();
            secKillSessionAndSecKillSkuVo.setSessionId(smsSeckillSession.getId());
            secKillSessionAndSecKillSkuVo.setSessionName(smsSeckillSession.getName());
            secKillSessionAndSecKillSkuVo.setSessionStartTime(smsSeckillSession.getStartTime());
            secKillSessionAndSecKillSkuVo.setSessionEndTime(smsSeckillSession.getEndTime());
            //根据场次对应的商品信息，此接口拿不到商品的名字、商品的销售属性与图片
            long sessionId=smsSeckillSession.getId();
            LambdaQueryWrapper<SmsSeckillSkuRelation> wrapper2=new LambdaQueryWrapper<>();
            wrapper2.eq(SmsSeckillSkuRelation::getSessionId,sessionId);
            List<SmsSeckillSkuRelation> smsSeckillSkuRelationList=smsSeckillSkuRelationMapper.selectList(wrapper2);
            //根据商品的id，查询商品的信息(需要远程调用,调用sku远程接口)
            List<SecKillSkuVo> list2=smsSeckillSkuRelationList.stream().map(smsSeckillSkuRelation -> {
                SecKillSkuVo secKillSkuVo=new SecKillSkuVo();
                secKillSkuVo.setId(smsSeckillSkuRelation.getId());
                secKillSkuVo.setSkuId(smsSeckillSkuRelation.getSkuId());
                Result<SecKillSkuInfoVo> result=seckillSkuClient.detail1(smsSeckillSkuRelation.getSkuId());
                if (result.getData()!=null){
                    SecKillSkuInfoVo secKillSkuInfoVo=result.getData();
                    secKillSkuVo.setSkuName(secKillSkuInfoVo.getSkuName());
                    secKillSkuVo.setSecKillImage(secKillSkuInfoVo.getDefaultImage());
                    secKillSkuVo.setSkuSallAttrValue(secKillSkuInfoVo.getSaleAttrs());
                }
                String randomCode= UUID.randomUUID().toString().replace("-","");
                secKillSkuVo.setSkuRandomCode(randomCode);
                secKillSkuVo.setSecKillCount(smsSeckillSkuRelation.getSeckillCount().intValue());
                secKillSkuVo.setSecKillLimit(smsSeckillSkuRelation.getSeckillLimit().intValue());
                secKillSkuVo.setSecKillPrice(smsSeckillSkuRelation.getSeckillPrice());

                return secKillSkuVo;
            }).collect(Collectors.toList());
            secKillSessionAndSecKillSkuVo.setSecKillSkuVoList(list2);
            return secKillSessionAndSecKillSkuVo;
        }).collect(Collectors.toList());
        return list;
    }

    public List<SmsSeckillSession> getSmsSeckillSessions() {
        //今天的开始时间
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        Date startTime=calendar.getTime();
        //今天的结束时间
        Calendar calendar2=Calendar.getInstance();
        calendar2.set(Calendar.HOUR_OF_DAY,23);
        calendar2.set(Calendar.MINUTE,59);
        calendar2.set(Calendar.SECOND,59);
        Date endTime=calendar2.getTime();
        //获得今天的场次信息
        LambdaQueryWrapper<SmsSeckillSession> wrapper=new LambdaQueryWrapper<>();
        wrapper.ge(SmsSeckillSession::getStartTime,startTime);
        wrapper.le(SmsSeckillSession::getEndTime,endTime);
        List<SmsSeckillSession> smsSeckillSessionList=smsSeckillSessionMapper.selectList(wrapper);
        return smsSeckillSessionList;
    }
}
